<template>
	<view :class="[primarySize()]">
		<view class="page-foot" style="z-index: 90;">
			<!-- 底部发消息 -->
			<view class="bom-box m-hairline--bottom">
				<view class="flex-box" :style="{paddingBottom:`${chatHeight}px`}">

					<image src="https://admin.mosizp.cn/static/public/icon_pic.png" @click="uploadImage" mode="aspectFit" class="pic-btn"
						style="margin-left: 0;"></image>
					<input placeholder="说点什么" placeholder-class="colc" class="write flex-grow-1" @input="bindInput"
						@blur="inputOff" @focus="inputOn" :value="messageText" :focus="inputFocus" confirm-type="send"
						@confirm="bindSend"></input>
					<image src="https://admin.mosizp.cn/static/public/icon_emoji.png" mode="aspectFit" class="face-btn" @click="showEmotion">
					</image>
					<image :src="iconPath('icon_send.png')" mode="aspectFit" class="pic-btn" @click="bindSend"></image>
				</view>
			</view>
			<!-- 表情 -->
			<view class="express" :class="[showEmoji?'':'toMax']">
				<view class="express-inner">
					<block v-for="(item,index) in emotion_json">
						<image class="emotion" @click="chooseEmoji(index, item)" :src="item.image">
						</image>
					</block>
				</view>
			</view>

			<view class="usertext_wrap" :class="[isShowText ? '' : 'hide']">
				<view class="usertext_inner">
					<view class="text_item single-line m-hairline--bottom" @click="chooseSentence(index, item)"
						v-for="(item,index) in sentenceList" :key="index">
						{{item.name}}
					</view>
				</view>
				<view class="usertext_foot m-hairline--top flex-box">
					<navigator url="/pages/public/communicate_word" hover-class="none" class="flex-grow-1">
						<image src="https://admin.mosizp.cn/static/public/icon_usertext2.png" class="icon-user"></image>
						常用语管理
					</navigator>
				</view>
			</view>

		</view>

		<!-- 头部 -->
		<view class="page-head">
			<view class="opt-top flex-box">
				<navigator hover-class="none" v-if="userinfo.group_id == 1" class="flex-grow-1 opt-item"
					:url="'/pages/user/company_info?id=' + chatParams.company_id">
					<image src="https://admin.mosizp.cn/static/public/chat1.png" class="opt-icon" mode="aspectFit"></image>
					<view>看主页</view>
				</navigator>
				<navigator hover-class="none" v-if="userinfo.group_id == '2'" class="flex-grow-1 opt-item"
					:url="'/pages/company/talent_details?id='+chatParams.resume_id">
					<image src="https://admin.mosizp.cn/static/public/chat1.png" class="opt-icon" mode="aspectFit"></image>
					<view>看简历</view>
				</navigator>
				<navigator hover-class="none" v-if="userinfo.group_id == '2'" class="flex-grow-1 opt-item"
					:url="'/pages/company/choose_job?receive_user_id='+chatParams.receive_user_id">
					<image src="https://admin.mosizp.cn/static/public/chat9.png" class="opt-icon" mode="aspectFit"></image>
					<view>发职位</view>
				</navigator>
				<!-- <view class="flex-grow-1 opt-item" bindtap="bindInterest">
					<image src="/images/icon_communicate7.png" class="opt-icon" mode="aspectFit"></image>
					<view>不感兴趣</view>
				</view> -->
				<!-- <view v-if="{{role == 'employee'}}" class="flex-grow-1 opt-item">
		      <image src="/images/icon_communicate3.png" class="opt-icon" mode="aspectFit"></image>
		      <view>发简历</view>
		    </view> -->
				<view v-if="userinfo.group_id == 1" class="flex-grow-1 opt-item" @click="bindApply">
					<image src="https://admin.mosizp.cn/static/public/chat3.png" class="opt-icon" mode="aspectFit"></image>
					<view>投递</view>
				</view>
				<view v-if="userinfo.group_id == 2" class="flex-grow-1 opt-item" @click="bindCreateInterview">
					<image src="https://admin.mosizp.cn/static/public/chat11.png" class="opt-icon opt-icon-5" mode="aspectFit"></image>
					<view>邀请面试</view>
				</view>
				<view class="flex-grow-1 opt-item" @click="bindCommunicateJob">
					<image src="https://admin.mosizp.cn/static/public/chat8.png" class="opt-icon" mode="aspectFit"></image>
					<view>已沟通职位</view>
				</view>
			</view>
		</view>
		<!-- 内容 -->
		<view class="container" catchtap="closeChat">

			<!-- 自动滚动到最底部 -->
			<scroll-view :scroll-top="scrollTop" scroll-with-animation class="scroll-wrap" scroll-y>
				<view class="scroll-main">
					<!-- 日期 -->

					<!-- 职位沟通 -->
					<view v-for="(item,index) in loadingList.data" :key="index">
						<view class="top_date">{{item.time}}</view>
						<block v-for="(msg,index2) in item.data" :key="index2">
							<view :id="'id'+index2" v-if="msg.type==1 || msg.type==2" class="flex-box"
								:class="msg.from_uid==chatParams.receive_user_id?'dialog-left':'dialog-right'">
								<image
									:src="cdnurl(msg.from_uid==chatParams.receive_user_id?msg.senduser.avatar:msg.senduser.avatar)"
									mode="aspectFill" class="head-pic"></image>
								<!-- 文字 -->
								<view class="dialog-text" v-if="msg.type==1" @longpress="bindToggle(index, index2, msg)">
									<block v-for="(v,index) in msg.message">
										<image :src="v.value" v-if="v.type==2" class="emoji"></image>
										<text v-if="v.type==1">{{v.value}}</text>
									</block>
									<view v-if="msg.from_uid!=chatParams.receive_user_id && msg.id == chatId"
										@click="revoke(index, index2)" class="btn_back">撤回
									</view>
								</view>
								<!-- 图片 -->
								<view class="dialog-text" v-if="msg.type==2" @longpress="bindToggle(index, index2, msg)">
									<image :src="msg.message" @click="previewImg(msg.message)"
										mode="widthFix" class="userPic"></image>
									<view v-if="msg.from_uid!=chatParams.receive_user_id && msg.id == chatId"
										class="btn_back" @click="revoke(index, index2)">撤回
									</view>
								</view>
							</view>

							<!-- 职位沟通卡片 -->
							<view @click="bindJobDetail(msg.message_text.id)" v-else-if="msg.type==3"
								class="job_card">
								<view class="card_body">
									<view class="flex-box">
										<view class="flex-grow-1 flex-box">
											<view class="job_name">{{msg.message_text.name}}</view>
											<!-- <view class="jipin" v-if="msg.message_text.in_hurry ==1">急招</view> -->
										</view>
										<view class="job_salary">{{msg.message_text.salary}}</view>
									</view>
									<view class="job_label flex-box">
										<!-- <view class="label"></view> -->
										<view class="label">{{msg.message_text.education_level}}</view>
										<view class="label">{{msg.message_text.work_experience}}</view>
										<view class="label" v-for="(item2,index) in msg.message_text.benefits">
											{{item2.name}}
										</view>
									</view>
								</view>
								<view class="card_foot flex-box">
									<image class="icon_company" :src="cdnurl(msg.message_text.company_logo)" mode="aspectFill">
									</image>
									<view class="flex-grow-1 ">
										<view class="flex-box">
											<view class="fs30 col3 flex-grow-1">{{msg.message_text.company_name}}</view>
											<view>{{msg.message_text.city}}{{msg.message_text.district}}</view>
										</view>
										<!-- <view class="mt10">周云/HR</view> -->
									</view>
								</view>
							</view>
							<!-- 投递 -->
							<view @click="bindJobDetail(msg.message_text.id)" v-else-if="msg.type==4"
								class="job_card">
								<view class="card_body">
									<view class="flex-box">
										<view class="flex-grow-1 flex-box">
											<view class="job_name">{{msg.message_text.name}}</view>
											<!-- <view class="jipin" v-if="msg.message_text.in_hurry ==1">急招</view> -->
										</view>
										<view class="job_salary">{{msg.message_text.salary}}</view>
									</view>
									<view class="job_label flex-box">
										<view class="label">{{msg.message_text.city}}{{msg.message_text.district}}
										</view>
										<view class="label">{{msg.message_text.education_level}}</view>
										<view class="label">{{msg.message_text.work_experience}}</view>
										<view class="label" v-for="(item2,indx2) in msg.message_text.benefits">
											{{item2.name}}
										</view>
									</view>
								</view>
								<view class="card_foot flex-box">
									<!-- <image class="icon_company" src="" mode="aspectFill"></image> -->
									<view class="flex-grow-1">
										{{userinfo.group_id==1?'投递成功':'对方已投递'}}
									</view>
								</view>
							</view>
							<!-- 面试邀请 -->
							<view @click="bindJobDetail(msg.message_text.job_id)" v-if="msg.type==6"
								class="job_card">
								<view class="card_body">
									<view class="flex-box">
										<view class="flex-grow-1 flex-box">
											<view class="job_name">{{msg.message_text.name}}</view>
											<!-- <view class="jipin" v-if="msg.message_text.in_hurry ==1">急招</view> -->
										</view>
										<view class="job_salary">{{msg.message_text.salary}}</view>
									</view>
									<view class="job_label flex-box">
										<!-- <view class="label">{{msg.message_text.city}}{{msg.message_text.district}}</view> -->
										<view class="label">{{msg.message_text.education_level}}</view>
										<view class="label">{{msg.message_text.work_experience}}</view>
										<view class="label" v-for="(item2,index2) in msg.message_text.benefits">
											{{item2.name}}
										</view>
									</view>
								</view>
								<view class="card_foot flex-box  m-hairline--bottom">
									<image class="icon_company" :src="cdnurl(msg.message_text.company_logo)"
										mode="aspectFill"></image>
									<view class="flex-grow-1 ">
										<view class="flex-box">
											<view class="fs30 col3 flex-grow-1">{{msg.message_text.company_name}}</view>
											<view>{{msg.message_text.city}}{{msg.message_text.district}}</view>
										</view>
										<!-- <view class="mt10">周云/HR</view> -->
									</view>
								</view>
								<view class="interview_box">
									<view class="title1 mb30">面试信息</view>
									<view class="flex-box mb30">
										<view class="title2">面试时间</view>
										<view class="flex-grow-1 val tr">
											{{formatDatetime(msg.message_text.interview_time,'yyyy.mm.dd hh:ii')}}
										</view>
									</view>
									<view class="flex-box">
										<view class="title2">面试地址</view>
										<view class="flex-grow-1 val">{{msg.message_text.interview_address}}</view>
									</view>
									<view class="btn_wrap flex-box"
										v-if="!msg.message_text.status && userinfo.group_id == 1">
										<view class="zbtn1" @click.stop="bindRejectInterview(index, index2)">拒绝面试</view>
										<view class="zbtn2" @click.stop="bindAcceptInterview(index, index2)">同意面试</view>
									</view>
									<view class="btn_wrap flex-box"
										style="justify-content: space-between;padding: 0 80rpx;" v-else>
										<view v-if="msg.message_text.status == 1" class="zbtn1">拒绝面试</view>
										<view v-if="msg.message_text.status == 2" class="zbtn2">同意面试</view>
									</view>
								</view>
							</view>
						</block>

					</view>

				</view>
			</scroll-view>
		</view>

	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				messageText: '',
				chatParams: {},
				userinfo: {},
				emotion_json: [], //表情
				loadingList: {
					total: 0,
					current_page: 0,
					last_page: 1,
					data: []
				},
				chatId: 0,
				showEmoji: false,
				inputFocus: false,
				refreshSessionStatus: true,
				cursor: 0,
				scrollTop: 0,
				chatHeight: 0,
				isShowText: false,
				sentenceList: []
			}
		},
		onReady() {
			uni.setNavigationBarColor({
		
				frontColor: '#ffffff',
		
				backgroundColor: this.primaryColor()
		
			})
		},
		onLoad(options) {
			// 检查并确保 WebSocket 已连接
			console.log('chat页面onLoad - 开始检查WebSocket连接状态');
			const userinfo = this.$core.getUserinfo();
			if (userinfo) {
				const app = getApp();
				const socketTask = app.globalData.chat_config.SocketTask;
				console.log('chat页面onLoad - 用户已登录，SocketTask状态:', socketTask ? socketTask.readyState : 'null');

				// 如果用户已登录但 WebSocket 未连接或连接已断开
				if (!socketTask || socketTask.readyState !== 1) {
					console.log('chat页面onLoad - WebSocket未连接，现在建立连接...');
					this.$chatIM.connectSocket();
				} else {
					console.log('chat页面onLoad - WebSocket已连接，状态正常');
				}
			} else {
				console.log('chat页面onLoad - 用户未登录');
			}


			uni.$off('new_msg_callback')
			uni.$off('msg_list_callback');
			uni.$off('del_session');
			uni.$off('del_chat');
			if (this.refreshSessionStatus) {
			  //this.getOpenerEventChannel().emit("refreshSession",{});
			  uni.$emit("refresh_session",{});
			  uni.$emit(app.globalData.Events.WebsocketMessageCount,{})
			}



			var page = this;
			//表情
			this.userinfo = this.$core.getUserinfo();
			this.emotion_json = app.globalData.chat_config.emoList
			let chatParams = JSON.parse(options.param);
			page.chatParams = chatParams;
			uni.setNavigationBarTitle({
				title: chatParams.to_username
			})
			page.refreshSessionStatus = chatParams.refresh_status || false;
			//获取当前的聊天记录
			page.downCallback();
			//查询聊天进程与聊天记录
			if (chatParams.content) {
				this.$chatIM.sendMessage(chatParams.receive_user_id, chatParams.content, chatParams.chat_type);
			}
			// this.getOpenerEventChannel().on('chatTransfor', function(chatParams) {
			// 	page.chatParams = chatParams
			// 	page.refreshSessionStatus = chatParams.refresh_status || false

			// 	uni.setNavigationBarTitle({
			// 		title: chatParams.to_username
			// 	})
			// 	//获取当前的聊天记录
			// 	page.downCallback();
			// 	//查询聊天进程与聊天记录
			// 	if (chatParams.content) {
			// 		this.$chatIM.sendMessage(chatParams.receive_user_id, chatParams.content, chatParams.chat_type);
			// 	}
			// })
			uni.$on('msg_list_callback', function(data) {
				page.msgList(data);
			})
			uni.$on('new_msg_callback', function(data) {
				page.refreshSessionStatus = true;
				page.newMsg(data);
			})
			uni.$on('del_session', function(data) {
				page.refreshSessionStatus = true;
				//刷新列表
				uni.showModal({
					title: '提示',
					content: '设置成功',
					showCancel: false,
					success: function() {
						uni.navigateBack({});
					}
				})
			})
			//删除聊天记录
			uni.$on("del_chat", function(data) {
				page.downCallback();
				return '';
				let loadingList = page.loadingList;
				let index = -1,
					index2 = -1;
				for (let i = 0; i < loadingList.data.length; i++) {
					for (let j = 0; j < loadingList.data[i].data.length; j++) {
						if (loadingList.data[i].data[j].id == data.id) {
							index = i;
							index2 = j;
							break;
						}
					}
				}
				let lists = loadingList.data[index];
				if (index >= 0 && index2 >= 0) {
					lists.data.splice(index2, 1);
					loadingList.data[index] = lists.data.length > 0 ? lists : []
				}
				page.loadingList = loadingList
				page.refreshSessionStatus = true

			})

		},
		onShow() {
			// 页面显示时检查是否有待显示的职位卡片
			console.log('chat页面onShow被触发');
			const app = getApp();
			if (app.globalData.pendingJobCard) {
				const pendingData = app.globalData.pendingJobCard;
				console.log('发现待处理的职位卡片:', pendingData);

				// 检查是否是当前聊天对象
				if (pendingData.receive_user_id == this.chatParams.receive_user_id) {
					console.log('匹配当前聊天对象，立即显示职位卡片');
					this.addJobCard(pendingData.job);
				} else {
					console.log('不是当前聊天对象，忽略');
				}

				// 清除全局变量，避免重复处理
				delete app.globalData.pendingJobCard;
			} else {
				console.log('没有待处理的职位卡片');
			}

			// 检查待显示的面试邀请卡片
			if (app.globalData.pendingInterviewCard) {
				const pendingData = app.globalData.pendingInterviewCard;
				console.log('发现待处理的面试邀请卡片:', pendingData);

				// 检查是否是当前聊天对象
				if (pendingData.receive_user_id == this.chatParams.receive_user_id) {
					console.log('匹配当前聊天对象，立即显示面试邀请卡片');
					this.addInterviewCard(pendingData.interview, pendingData.job);
				} else {
					console.log('不是当前聊天对象，忽略');
				}

				// 清除全局变量，避免重复处理
				delete app.globalData.pendingInterviewCard;
			} else {
				console.log('没有待处理的面试邀请卡片');
			}
		},
		onHide() {
			uni.$off('new_msg_callback')
			uni.$off('msg_list_callback');
			uni.$off('del_session');
			uni.$off('del_chat');
			if (this.refreshSessionStatus) {
			  //this.getOpenerEventChannel().emit("refreshSession",{});
			  uni.$emit("refresh_session",{});
			  uni.$emit(app.globalData.Events.WebsocketMessageCount,{})
			}
		},
		onUnload() {
		    uni.$off('new_msg_callback')
		    uni.$off('msg_list_callback');
		    uni.$off('del_session');
		    uni.$off('del_chat');
		    if (this.refreshSessionStatus) {
			  //this.getOpenerEventChannel().emit("refreshSession",{});
			  uni.$emit("refresh_session",{});
			  uni.$emit(app.globalData.Events.WebsocketMessageCount,{})
		    }
		  },
		methods: {
			//聊天滑动
			pageScrollToBottom() {
				const query = uni.createSelectorQuery().in(this);
				query.select('.scroll-wrap').boundingClientRect();
				setTimeout(()=>{
				query.select('.scroll-main').boundingClientRect();
				query.exec(res => {
					const scorllHeight = res[0].height;
					const listHeight = res[1].height;
					this.scrollTop =scorllHeight>=listHeight? scorllHeight + 100:listHeight+100
					this.$nextTick()
				});
					
				},500)
			},
			// 投递简历
			bindApply() {
				if (!this.$core.getUserinfo(true)) {
					return;
				}
				let page = this;
				this.$core.post({
					url: 'xilujob.job/chat_job',
					data: {
						receive_user_id: page.chatParams.receive_user_id
					},
					success: data => {
						this.$core.post({
							url: 'xilujob.job/apply',
							data: {
								job_id: data.data
							},
							success: ret => {
								uni.showModal({
									title: '提示',
									content: '投递成功',
									showCancel: false,
								});
								//发送投递成功的消息
								page.$chatIM.sendMessage(page.chatParams.receive_user_id, ret.data.id, 4);
								page.pageScrollToBottom();
								return false;
							},
							fail: ret => {
								if (ret && ret.code == -1) {
									uni.showModal({
										title: '提示',
										content: '需要先完善简历，现在要去吗？',
										cancelText: '先看看',
										confirmText: '立刻完善',
										success: res => {
											if (res.confirm) {
												uni.navigateTo({
													url: '/pages/user/my_resume'
												});
											}
										}
									});
									return false;
								}
							}
						});
					}
				})
			},
			//不感兴趣
			bindInterest() {
				let page = this;
				uni.showModal({
					title: '提示',
					content: '确定不感兴趣？',
					cancelText: '再聊聊',
					confirmText: '不感兴趣',
					success: res => {
						if (res.confirm) {
							page.$chatIM.sendMessage(this.chatParams.receive_user_id, "暂时对您不感兴趣");
						}

					}
				});
			},
			//沟通职位
			bindCommunicateJob() {
				uni.navigateTo({
					url: '/pages/public/chat_job?receive_user_id=' + this.chatParams.receive_user_id,
				})
			},
			// 获取聊天列表
			msgList(res) {
				var loadingList = res.data;
				// 获取当前最顶部的VIEW_ID (注意是写在data.concat前面)


				if (this.loadingList.current_page > 1) {
					var now_loadingList_data = this.loadingList.data || [];
					var now_loadingList_data = now_loadingList_data.concat(loadingList.data);
					loadingList.data = now_loadingList_data;
				}
				this.loadingList = loadingList
				this.pageScrollToBottom();

				let receive_user_id = res.receive_user ? res.receive_user.id : 0;
				this.$chatIM.wsSend({
					type: 'setread',
					receive_user_id: receive_user_id
				});
			},
			// 新消息
			newMsg(data) {
				if (data.to_uid != this.chatParams.receive_user_id && data.from_uid != this.chatParams.receive_user_id) {
					return false;
				}
				var loadingList = this.loadingList;
				var now_loadingList_data = loadingList.data || [];
				var ret = this.$chatIM.handle_msg_data(data);
				let lastData = now_loadingList_data.length>0 ? now_loadingList_data[0] : null;
				if(lastData && lastData.id == ret.data[0].data[0].id){
					return false;
				}
				var now_loadingList_data = now_loadingList_data.concat(ret.data);
				loadingList.data = now_loadingList_data;
				this.refreshSessionStatus = true
				this.loadingList = loadingList

				this.pageScrollToBottom();
				if (data.from_uid == this.chatParams.receive_user_id) {
					this.$chatIM.wsSend({
						type: 'setread',
						receive_user_id: this.chatParams.receive_user_id
					});
				}
			},
			/*下拉刷新的回调 */
			downCallback() {
				//websocket获取列表
				this.$chatIM.get_msg_list(this.chatParams.receive_user_id, 1);
			},
			//职位详情
			bindJobDetail(jobId) {
				//企业不可看职位详情
				if (this.userinfo.group_id == 2) {
					return '';
				}
				uni.navigateTo({
					url: '/pages/user/post_info?id=' + jobId,
				})
			},
			// 聚焦输入框
			inputOn(e) {
				this.inputFocus = true
				this.showEmoji = false
				//console.log(this.keyboardH)
				//滑动在这编辑
				//this.mescroll.scrollTo(99999, 0);
			},
			// 失焦输入框
			inputOff() {
				this.inputFocus = false;
			},
			//常用语
			toggleUserText: function() {
				this.isShowText = !this.isShowText
				this.showEmoji = false
				if (this.isShowText) {
					this.getSentence()
				}
			},
			//常用语
			getSentence() {
				this.$core.get({
					url: 'xilujob.sentence',
					loading: false,
					success: ret => {
						this.sentenceList = ret.data

					}
				});
			},
			chooseSentence(index, item) {
				this.messageText = item.name

			},
			//表情
			showEmotion() {
				this.isShowText = false
				this.showEmoji = !this.showEmoji
			},
			//选择表情
			chooseEmoji(index, item) {
				this.messageText = this.messageText + this.emotion_json[index].name
			},
			// 输入内容
			bindInput(e) {
				this.messageText = e.detail.value
			},
			//发送
			bindSend() {
				let content = this.messageText;
				if (content.trim() == '') {
					uni.showToast({
						title: "发送内容不得为空",
						icon: 'none'
					});
					return false;
				}
				this.$chatIM.sendMessage(this.chatParams.receive_user_id, content);
				this.messageText = ''
				this.showEmoji = false
				//this.mescroll.scrollTo(99999, 0);
			},
			//发送图片消息
			uploadImage() {
				let page = this;
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['album', 'camera'],
					success(res) {
						res.tempFiles.forEach(function(file) {
							page.$core.uploadFile({
								filePath: file.tempFilePath,
								success(ret, response) {
									page.$chatIM.sendMessage(page.chatParams.receive_user_id, ret.data.url, 2);
								}
							});
						});
					}
				})
			},
			//长按
			bindToggle(index, index2, msg) {
				let loadingList = this.loadingList;
				this.chatId = loadingList.data[index].data[index2].id
			},
			//消息撤回
			revoke(index, index2) {
				let loadingList = this.loadingList;
				let id = loadingList.data[index].data[index2].id;
				this.$chatIM.wsSend({
					type: 'delchat',
					receive_user_id: this.chatParams.receive_user_id,
					content: id
				});
			},
			//面试邀请
			bindCreateInterview() {
				let userinfo = this.$core.getUserinfo();
				if (userinfo.group_id != 2) {
					uni.showToast({title: '请切换到公司身份',icon: 'none'});
					return;
				}
				let resumeId = this.chatParams.resume_id;
				this.$core.post({
					url: 'xilujob.resume/get_interview',
					data: {
						resume_id: resumeId
					},
					success: ret => {
						uni.navigateTo({
							url: '/pages/company/invite_interview',
							events: {
								dataTransfer: data => {

									this.isInterviewing = true

								}
							},
							success: res => {
								res.eventChannel.emit('dataTransfer', {
									resume: ret.data
								});
							}
						});
					},
					fail: ret => {
						if (ret && ret.code == -1) {
							this.actionTipMsg = ret.msg
							return false;
						}
					}
				});
			},
			//拒绝面试
			bindRejectInterview(index, index2) {
				var that = this;
				let loadingList = this.loadingList;
				let id = loadingList.data[index].data[index2].message_text.id;
				uni.showModal({
					title: '提示',
					content: '确认拒绝面试？',
					success: res => {
						if (res.confirm) {
							that.$core.post({
								url: 'xilujob.job_interview/multi_status/interview_id/' + id,
								data: {
									status: 1
								},
								success: ret => {
									loadingList.data[index].data[index2].message_text.status = 1;
									that.loadingList = that.loadingList;
									that.$forceUpdate()
									that.$nextTick()
								}
							});
						}
					}
				})
			},
			bindAcceptInterview(index, index2) {
				let that = this;
				let loadingList = this.loadingList;
				let id = loadingList.data[index].data[index2].message_text.id;
				uni.showModal({
					title: '提示',
					content: '确认接受面试？',
					success: res => {
						if (res.confirm) {
							this.$core.post({
								url: 'xilujob.job_interview/multi_status/interview_id/' + id,
								data: {
									status: 2
								},
								success: ret => {
									loadingList.data[index].data[index2].message_text.status = 2;
									that.$forceUpdate()
									this.$nextTick()
								}
							});
						}
					}
				})
			},
			previewImg(url){
				uni.previewImage({
					urls: [url],
					current: 0
				})
			},
			// 添加职位卡片到聊天界面（立即更新UI）
			addJobCard(job) {
				console.log('收到发送职位事件，职位数据:', job);

				const userinfo = this.$core.getUserinfo();
				// 获取当前时间，格式：2024.01.15 10:30
				const now = new Date();
				const year = now.getFullYear();
				const month = String(now.getMonth() + 1).padStart(2, '0');
				const day = String(now.getDate()).padStart(2, '0');
				const hour = String(now.getHours()).padStart(2, '0');
				const minute = String(now.getMinutes()).padStart(2, '0');
				const currentTime = `${year}.${month}.${day} ${hour}:${minute}`;

				// 构造职位卡片消息数据
				const jobCardMsg = {
					id: Date.now(), // 临时ID
					type: 3, // 职位卡片类型
					from_uid: userinfo.id,
					to_uid: this.chatParams.receive_user_id,
					message_text: {
						id: job.id,
						name: job.job_name || job.name,
						salary: job.salary_text || job.salary,
						education_level: (job.education_level && job.education_level.name) || job.education_level || '',
						work_experience: (job.work_experience && job.work_experience.name) || job.work_experience || '',
						benefits: job.welfare_list || job.benefits || [],
						company_logo: (job.company && job.company.logo) || job.company_logo || '',
						company_name: (job.company && job.company.company_name) || job.company_name || '',
						city: (job.city && job.city.name) || job.city || '',
						district: (job.district && job.district.name) || job.district || ''
					},
					senduser: {
						avatar: userinfo.avatar
					}
				};

				console.log('构造的职位卡片消息:', jobCardMsg);

				// 获取当前聊天记录
				var loadingList = this.loadingList;
				var now_loadingList_data = loadingList.data || [];

				// 如果已有消息，添加到最后一组；否则创建新数组
				if (now_loadingList_data.length > 0) {
					// 获取最后一组消息
					let lastGroup = now_loadingList_data[now_loadingList_data.length - 1];
					// 直接添加到最后一组的data数组中
					lastGroup.data.push(jobCardMsg);
					console.log('添加到现有分组，分组时间:', lastGroup.time);
				} else {
					// 创建新的消息分组
					const newGroup = {
						time: currentTime,
						data: [jobCardMsg]
					};
					now_loadingList_data = [newGroup];
					console.log('创建新分组，分组时间:', currentTime);
				}

				// 更新数据并滚动到底部
				loadingList.data = now_loadingList_data;
				this.loadingList = loadingList;
				this.refreshSessionStatus = true;

				console.log('更新后的loadingList:', this.loadingList);

				// 强制更新并滚动到底部
				this.$forceUpdate();
				this.$nextTick(() => {
					console.log('准备滚动到底部');
					this.pageScrollToBottom();
				});
			},
			// 添加面试邀请卡片到聊天界面（立即更新UI）
			addInterviewCard(interview, job) {
				console.log('收到发送面试邀请事件，面试数据:', interview, '职位数据:', job);

				const userinfo = this.$core.getUserinfo();
				// 获取当前时间，格式：2024.01.15 10:30
				const now = new Date();
				const year = now.getFullYear();
				const month = String(now.getMonth() + 1).padStart(2, '0');
				const day = String(now.getDate()).padStart(2, '0');
				const hour = String(now.getHours()).padStart(2, '0');
				const minute = String(now.getMinutes()).padStart(2, '0');
				const currentTime = `${year}.${month}.${day} ${hour}:${minute}`;

				// 构造面试邀请卡片消息数据
				const interviewCardMsg = {
					id: Date.now(), // 临时ID
					type: 6, // 面试邀请类型
					from_uid: userinfo.id,
					to_uid: this.chatParams.receive_user_id,
					message_text: {
						id: interview.id,
						job_id: job ? job.id : interview.job_id,
						name: job ? (job.job_name || job.name) : interview.job_name,
						salary: job ? (job.salary_text || job.salary) : interview.salary,
						education_level: job ? ((job.education_level && job.education_level.name) || job.education_level || '') : interview.education_level,
						work_experience: job ? ((job.work_experience && job.work_experience.name) || job.work_experience || '') : interview.work_experience,
						benefits: job ? (job.welfare_list || job.benefits || []) : (interview.benefits || []),
						company_logo: job ? ((job.company && job.company.logo) || job.company_logo || '') : interview.company_logo,
						company_name: job ? ((job.company && job.company.company_name) || job.company_name || '') : interview.company_name,
						city: job ? ((job.city && job.city.name) || job.city || '') : interview.city,
						district: job ? ((job.district && job.district.name) || job.district || '') : interview.district,
						interview_time: interview.interview_time,
						interview_address: interview.interview_address || interview.address,
						status: 0 // 0=面试邀请,1=用户拒绝,2=用户同意
					},
					senduser: {
						avatar: userinfo.avatar
					}
				};

				console.log('构造的面试邀请卡片消息:', interviewCardMsg);

				// 获取当前聊天记录
				var loadingList = this.loadingList;
				var now_loadingList_data = loadingList.data || [];

				// 如果已有消息，添加到最后一组；否则创建新数组
				if (now_loadingList_data.length > 0) {
					// 获取最后一组消息
					let lastGroup = now_loadingList_data[now_loadingList_data.length - 1];
					// 直接添加到最后一组的data数组中
					lastGroup.data.push(interviewCardMsg);
					console.log('添加到现有分组，分组时间:', lastGroup.time);
				} else {
					// 创建新的消息分组
					const newGroup = {
						time: currentTime,
						data: [interviewCardMsg]
					};
					now_loadingList_data = [newGroup];
					console.log('创建新分组，分组时间:', currentTime);
				}

				// 更新数据并滚动到底部
				loadingList.data = now_loadingList_data;
				this.loadingList = loadingList;
				this.refreshSessionStatus = true;

				console.log('更新后的loadingList:', this.loadingList);

				// 强制更新并滚动到底部
				this.$forceUpdate();
				this.$nextTick(() => {
					console.log('准备滚动到底部');
					this.pageScrollToBottom();
				});
			}

		}

	}
</script>

<style lang="scss" scoped>
	.container {
		background-color: rgb(246, 247, 250);
	}

	.page-head {
		background-color: #fff;
	}

	.page-head~.container {
		padding-top: 145rpx;
	}

	.page-foot~.container {
		padding-bottom: 100rpx;
	}

	@supports (bottom: constant(safe-area-inset-bottom)) or (bottom: env(safe-area-inset-bottom)) {
		.page-foot {
			padding-bottom: calc(34px/2);
			padding-bottom: calc(constant(safe-area-inset-bottom)/2);
			padding-bottom: calc(env(safe-area-inset-bottom)/2);
		}

		.page-foot~.container {
			padding-bottom: calc(34px/2 + 130rpx);
			padding-bottom: calc(constant(safe-area-inset-bottom)/2 + 130rpx);
			padding-bottom: calc(env(safe-area-inset-bottom)/2 + 130rpx);
		}
	}

	/* 顶部按钮 */
	.opt-top {
		color: #333333;
		font-size: 24rpx;
		font-weight: 400;
		line-height: 29rpx;
		text-align: center;
	}

	.opt-top .opt-item {
		padding: 25rpx 0 30rpx;
	}

	.opt-top .opt-icon {
		display: block;
		margin: 0 auto 17rpx;
		width: 50rpx;
		height: 50rpx;
	}

	/* 底部输入 */
	.bom-box {
		position: relative;
		background-color: #fff;
		padding: 14rpx 20rpx;
		min-height: 130rpx;
		box-sizing: border-box;
		align-items: flex-start;
	}

	.bom-box .write {
		margin: 0 0 0 10rpx;
		padding: 0 20rpx;
		display: block;
		background: #f7f8f9;
		border-radius: 15rpx;
		height: 70rpx;
		line-height: 70rpx;
	}

	.bom-box .pic-btn {
		margin-left: 10rpx;
		display: block;
		width: 70rpx;
		height: 70rpx;
		border-radius: 50%;
	}

	.bom-box .face-btn {
		margin: 0 0 0 10rpx;
		display: block;
		width: 70rpx;
		height: 70rpx;
		border-radius: 50%;
	}

	.btn_text {
		padding: 0 20rpx;
		font-size: 28rpx;
		color: #fff;
		background-color: var(--primary);
		height: 70rpx;
		line-height: 70rpx;
		border-radius: 8rpx;
	}

	.bom-box .icon_keyboard {
		display: block;
		width: 70rpx;
		height: 70rpx;
	}

	/* 新增常用语 */
	.usertext_wrap {
		padding: 0 30rpx;
		height: 570rpx;
		transition: all 0.3s;
		background-color: #fff;
	}

	.usertext_wrap.hide {
		height: 0;
		overflow: hidden;
		background-color: #fff;
	}

	.usertext_wrap .usertext_inner {
		height: 450rpx;
		overflow-y: auto;
		-webkit-overflow-scrolling: touch;
	}

	.usertext_wrap .text_item {
		padding: 40rpx 0;
		color: #555555;
		font-size: 30rpx;
		font-weight: 400;
		line-height: 32rpx;
	}

	.usertext_wrap .usertext_foot {
		height: 120rpx;
		color: #101010;
		font-size: 30rpx;
		font-weight: 400;
		line-height: 120rpx;
		text-align: center;
	}

	.usertext_wrap .usertext_foot .flex-grow-1 {
		position: relative;
		height: 120rpx;
	}

	.usertext_wrap .usertext_foot .flex-grow-1:first-of-type::after {
		content: '';
		position: absolute;
		right: 0;
		top: 45rpx;
		width: 1rpx;
		height: 30rpx;
		background: #eeeeee;
	}

	.usertext_wrap .icon-user {
		margin: 0 10rpx 0 0;
		display: inline-block;
		width: 35rpx;
		height: 35rpx;
		vertical-align: -4rpx;
	}

	/* 职位卡片 */
	.job_card {
		margin: 30rpx 0 0;
		border-radius: 5rpx;
		border: 1rpx solid #ebebeb;
		background: #ffffff;
	}

	.job_card .card_body {
		padding: 25rpx 30rpx 15rpx;
	}

	.job_card .job_name {
		margin: 0 0 15rpx;
		color: #101010;
		font-size: 34rpx;
		font-weight: 400;
		line-height: 41rpx;
	}

	.job_card .job_salary {
		margin: 0 0 20rpx;
		color: var(--primary);
		font-size: 30rpx;
		font-weight: 400;
		line-height: 36rpx;
	}

	.job_card .job_label {
		flex-wrap: wrap;
	}

	.job_card .label {
		margin: 0 13rpx 15rpx 0;
		padding: 8rpx 18rpx;
		border-radius: 8rpx;
		background: #f7f7f7;
		color: #999999;
		font-size: 20rpx;
		font-weight: 400;
		line-height: 24rpx;
	}

	.job_card .card_foot {
		padding: 30rpx;
		color: #101010;
		font-size: 24rpx;
		font-weight: 400;
		// line-height: 26rpx;
		border-top: 1px solid #eee;
	}

	.job_card .icon_company {
		margin: 0 6rpx 0 0;
		display: block;
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
	}

	.job_card .date {
		color: #999999;
	}

	/* 内容消息 */
	.dialog-left,
	.dialog-right {
		padding-top: 40rpx;
	}

	.head-pic {
		display: block;
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		align-self: flex-start;
	}

	.dialog-text {
		position: relative;
		margin: 0 20rpx;
		padding: 17rpx 22rpx;
		font-size: 30rpx;
		line-height: 43rpx;
		max-width: 490rpx;
		font-weight: 500;
	}

	.dialog-left .dialog-text {
		background: #ffffff;
		border-radius: 15rpx;
		color: #101010;
	}

	.dialog-right {
		flex-direction: row-reverse;
	}

	.dialog-right .dialog-text {
		background: var(--primary);
		border-radius: 15rpx;
		color: #fff;
	}

	.dialog-text .emoji {
		margin: 0 10rpx 0 0;
		display: inline-block;
		width: 50rpx;
		height: 50rpx;
	}

	.dialog-text text {
		vertical-align: top;
	}

	.dialog-text .btn_back {
		position: absolute;
		left: 50%;
		transform: translateX(-50%);
		top: -75rpx;
		width: 114rpx;
		height: 60rpx;
		background: rgba(0, 0, 0, 0.5);
		border-radius: 10rpx;
		text-align: center;
		line-height: 60rpx;
		font-weight: 400;
		font-size: 28rpx;
		color: #FFFFFF;
		font-style: normal;
	}

	.dialog-text .btn_back::after {
		position: absolute;
		bottom: -15rpx;
		left: 50%;
		transform: translateX(-50%);
		content: '';
		border-top: 14rpx solid rgba(0, 0, 0, 0.5);
		border-left: 16rpx solid transparent;
		border-right: 16rpx solid transparent;
	}

	/* 弹窗类 */
	.pop_mask {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.6);
		z-index: 200;
		opacity: 0;
		visibility: hidden;
	}

	.pop_wrap {
		position: fixed;
		opacity: 0;
		visibility: hidden;
		z-index: 201;
	}

	.pop_show {
		-webkit-transition-duration: 400ms;
		transition-duration: 400ms;
		opacity: 1;
		visibility: visible;
	}

	.pop-close {
		position: absolute;
		right: 30rpx;
		top: 45rpx;
		width: 34rpx;
		height: 34rpx;
	}

	.pop-close image {
		display: block;
		width: 34rpx;
		height: 34rpx;
	}

	/* 邀请弹窗 */
	.pop_invitation {
		bottom: 0;
		left: 0;
		padding: 40rpx 40rpx 60rpx;
		width: 100%;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 0 0;
		box-sizing: border-box;
	}

	.pop-title {
		padding: 0 75rpx 20rpx;
		font-size: 30rpx;
		color: #101010;
		text-align: center;
		line-height: 36rpx;
	}

	.test-group {
		padding: 40rpx 0;
		border-bottom: 1rpx solid #F4F3F3;
		font-size: 30rpx;
	}

	.test-name {
		color: #898989;
	}

	.test-address {
		margin-top: 20rpx;
	}

	.bom-btn {
		display: block;
		font-size: 30rpx;
		color: #fff;
		background: var(--primary);
		box-shadow: 0 4rpx 50rpx 0 rgba(74, 164, 219, 0.30);
		width: 600rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		border-radius: 40rpx 0 40rpx 40rpx;
		margin: 60rpx auto 0;
	}

	.h-send {
		margin-left: 20rpx;
	}

	.h-send image {
		display: block;
		width: 40rpx;
		height: 40rpx;
	}

	.say-icon {
		display: inline-block;
		width: 30rpx;
		height: 30rpx;
		vertical-align: -5rpx;
	}

	.dialog-left .say-icon {
		margin-right: 10rpx;
	}

	.dialog-right .say-icon {
		margin-right: 10rpx;
	}

	.hvoice {
		display: block;
		width: 100%;
		height: 100%;
		position: absolute;
		z-index: 30;
		left: 0;
		top: 0;
		opacity: 0;
	}

	/* 新增表情容器 */
	/* .page-foot{bottom: -400rpx;transition: all 0.3s;}
.page-foot.upAction{bottom: 0;} */
	.express {
		height: 310rpx;
		background-color: #f5f5f5;
		transition: all 0.3s;
		overflow-y: scroll;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
	}

	.express-inner {
		padding: 10rpx 30rpx 30rpx;
	}

	.express.toMax {
		height: 0;
		overflow: hidden;
		padding: 0;
		background-color: #fff;
	}



	.userPic {
		display: block;
		min-width: 50%;
		max-width: 450rpx;
	}

	.emotion {
		width: 50rpx;
		height: 50rpx;
		margin: 8rpx 6rpx;
	}

	.chat_image {
		width: 200rpx;
		height: 100rpx;
	}

	.topTip {
		background-color: var(--primary);
		height: 60rpx;
		line-height: 60rpx;
		font-size: 24rpx;
		padding: 0 20rpx;
		color: #fff;
		position: fixed;
		top: 0;
		z-index: 100000;
		width: 100%;
		text-align: center;
		font-weight: 400;
	}

	.topTip text {
		text-decoration: underline;
	}

	.topTip~.container {
		padding-top: 60rpx;
	}

	.topTip~.page-head {
		top: 60rpx;
	}

	.topTip~.page-head+.container {
		padding-top: 204rpx;
	}

	.container {
		height: 100vh;
	}

	.scroll-wrap {
		width: 100%;
		height: 100%;
	}

	.scroll-main {
		padding: 1rpx 30rpx 30rpx;
	}

	.scroll-main .top_date {
		margin: 0;
		color: #aaaaaa;
		font-size: 24rpx;
		font-weight: 400;
		line-height: 26rpx;
		text-align: center;
		padding-top: 30rpx;
	}

	.popup-recording {
		position: fixed;
		top: 50%;
		left: 50%;
		z-index: 99;
		margin-left: -140rpx;
		margin-top: -100rpx;
		padding: 40rpx;
		background-color: rgba(0, 0, 0, 0.5);
		border-radius: 20rpx;
	}

	.popup-recording image {
		display: block;
		margin: 20rpx auto 30rpx;
		width: 200rpx;
		height: auto;
	}

	.popup-recording text {
		display: block;
		text-align: center;
		line-height: 1;
		font-size: 28rpx;
		color: #fff;
	}




	.em-status {
		font-size: 34rpx;
		color: #17C9AB;
		align-self: flex-start;
		font-weight: bold;

	}

	.em-info {
		font-size: 24rpx;
		color: #999;
	}

	.em-tx {
		display: block;
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		overflow: hidden;
		margin-right: 20rpx;
	}

	.em-detail {
		font-size: 24rpx;
		color: #333;
		background-color: rgba(23, 201, 171, 0.04);
		padding: 10rpx 30rpx;
	}

	.em-detail_hd {
		font-size: 22rpx;
		color: #999;
	}

	.em-center_icon {
		width: 60rpx;
		height: 60rpx;
		background: #FE8260;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-right: 20rpx;
	}

	.em-center_icon image {
		display: block;
		width: 32rpx;
		height: 32rpx;
	}

	.z-name {
		font-size: 28rpx;
		font-weight: bold;
		color: #333333;
		line-height: 28rpx;
	}

	.z-job {
		font-size: 24rpx;
		font-weight: bold;
		color: #333333;
		line-height: 26px;
	}

	/* 收藏成功弹窗 */
	.success_pop {
		position: fixed;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		padding: 55rpx 0;
		width: 381rpx;
		border-radius: 30rpx;
		background: rgba(0, 0, 0, 0.8);
		color: #ffffff;
		font-size: 30rpx;
		font-weight: 400;
		line-height: 36rpx;
		text-align: center;
		z-index: 10;
		opacity: 0;
		visibility: hidden;
		transition: all 1s;
	}

	.success_pop.show {
		opacity: 1;
		visibility: visible;
	}

	.success_pop .icon_succ {
		margin: 0 auto 20rpx;
		display: block;
		width: 55rpx;
		height: 55rpx;
		border-radius: 50%;
	}


	/* 常用语弹窗 */
	.pop_add_text {
		bottom: 0;
		left: 0;
		padding: 40rpx 30rpx;
		width: 750rpx;
		height: 675rpx;
		border-radius: 2rpx;
		background: #ffffff;
		box-sizing: border-box;
	}

	.pop_add_text .textarea {
		width: 100%;
		height: 450rpx;
		color: #333;
		font-size: 30rpx;
		font-weight: 400;
		line-height: 36rpx;
	}

	.pop_add_text .pop_foot {
		position: absolute;
		bottom: 40rpx;
		width: 690rpx;
		color: #cccccc;
		font-size: 30rpx;
		font-weight: 400;
		line-height: 36rpx;
	}

	.pop_add_text .btn_text {
		padding: 0 30rpx;
		height: 60rpx;
		line-height: 60rpx;
	}

	/*  */
	.interview_box {
		padding: 30rpx 30rpx 40rpx;
	}

	.interview_box .title1 {
		font-size: 32rpx;
		font-weight: 500;
		color: #333333;
		line-height: 34rpx;
	}

	.interview_box .title2 {
		min-width: 5em;
		font-size: 30rpx;
		font-weight: 400;
		color: #898989;
		line-height: 32rpx;
	}

	.interview_box .val {
		font-size: 34rpx;
		font-weight: 400;
		color: #101010;
		line-height: 36rpx;
	}

	.interview_box .flex-box {
		align-items: flex-start;
	}

	.interview_box .btn_wrap {
		margin: 50rpx 0 0;
		justify-content: space-evenly;
	}

	.interview_box .zbtn1 {
		width: 200rpx;
		height: 70rpx;
		background: #FFA600;
		border-radius: 5rpx;
		font-size: 28rpx;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 70rpx;
		text-align: center;
	}

	.interview_box .zbtn2 {
		width: 200rpx;
		height: 70rpx;
		background: var(--primary);
		border-radius: 5rpx;
		font-size: 28rpx;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 70rpx;
		text-align: center;
	}
</style>